package com.cby.android.base.contentprovider

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.cby.android.base.contentprovider.Constant.DATABASE_NAME

/**
 * @description: DBHelper
 *
 * @author ChenBaoyang
 * @date 2021/10/13 17:25
 */
class DBHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, 1) {


    override fun onCreate(db: SQLiteDatabase?) {
        //创建表格
        /*db?.execSQL("CREATE TABLE IF NOT EXISTS "+ Constant.TABLE_NAME + "("+ Constant.COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
                + Constant.COLUMN_NAME +" VARCHAR NOT NULL," + Constant.COLUMN_AGE +" INTEGER);")*/

        db?.execSQL("create table if not exists user(id integer primary key AUTOINCREMENT, name varchar(20), age integer)")
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        // 这里知识简单删除并创建表格
        // 如果需要保留原来的数据，需要先备份再删除
        db?.execSQL("DROP TABLE IF EXISTS "+ Constant.TABLE_NAME+";");
        onCreate(db);
    }
}